package ar.uade.fain.ai.tpo.view.receta;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import ar.uade.fain.ai.tpo.Sistema;
import ar.uade.fain.ai.tpo.dto.CaracteristicaDTO;
import ar.uade.fain.ai.tpo.dto.IngredienteDTO;
import ar.uade.fain.ai.tpo.dto.ProductoDTO;
import ar.uade.fain.ai.tpo.dto.RecetaDTO;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class AltaReceta extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Long idIngrediente = 0L;
	private static Long idReceta = 0L;

	private JLabel nombreLabel;
	private JTextField nombreText;
	private JLabel descripcionLabel;
	private JButton altaRecetaButton;
	private JLabel complejidadLabel;
	private JComboBox complejidadCombo;
	private JTextField tCoccionText;
	private JLabel tCoccionLabel;
	private JTextArea descripcionArea;
	private JScrollPane scroll;
	private JTable ingrediente;
	private JCheckBox alHornojCheckBox1;
	private JTextField idRecetas;
	private JCheckBox esCompuesta;
	private JButton agregarIngredienteButton;
	private DefaultTableModel ingredienteModel;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AltaReceta inst = new AltaReceta();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public AltaReceta() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(
					(JComponent) getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				nombreLabel = new JLabel();
				nombreLabel.setText("Nombre:");
			}
			{
				nombreText = new JTextField();
			}
			{
				descripcionLabel = new JLabel();
				descripcionLabel.setText("Descripcion:");
			}
			{
				alHornojCheckBox1 = new JCheckBox();
				alHornojCheckBox1.setText("Al Horno");
			}
			{
				idRecetas = new JTextField();
				idRecetas.setEnabled(false);
			}
			{
				esCompuesta = new JCheckBox();
				esCompuesta.setText("Receta Compuesta");
				esCompuesta.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {

						idRecetas.setEnabled(esCompuesta.isSelected());

					}
				});
			}
			{
				descripcionArea = new JTextArea();
			}
			{
				tCoccionLabel = new JLabel();
				tCoccionLabel.setText("Tiempo de Coccion (min):");
			}
			{
				tCoccionText = new JTextField();
			}
			{
				ComboBoxModel complejidadComboModel = new DefaultComboBoxModel(
						new String[] { "Alta", "Media", "Baja" });
				complejidadCombo = new JComboBox();
				complejidadCombo.setModel(complejidadComboModel);
			}
			{
				complejidadLabel = new JLabel();
				complejidadLabel.setText("Compleijdad:");
			}
			{
				altaRecetaButton = new JButton();
				altaRecetaButton.setText("Aceptar");
				altaRecetaButton.addActionListener(new ActionListener() {

					@SuppressWarnings("unchecked")
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if (!nombreText.getText().isEmpty()) {
							boolean existenRecetas = true;
							final RecetaDTO recetaDTO = new RecetaDTO();
							String[] ids = null;
							if(esCompuesta.isSelected()){
								 ids = idRecetas.getText().split(" ");
								int i = ids.length;
								
								while(i<=0 && existenRecetas){
									existenRecetas =  Sistema.get().getComidaControlador().existeReceta(Long.parseLong(ids[i]));
									
									i--;
									
								}
								
								
							}
							
							if(existenRecetas){
							if(ids != null){
								for(int b = 0; b < ids.length;b++){
									recetaDTO.addRecetaContenida(Long.parseLong(ids[b]));
								}
							}
							final CaracteristicaDTO caracteristicaDTO = new CaracteristicaDTO();
							
							caracteristicaDTO.setDescripcion(descripcionArea
									.getText());
							caracteristicaDTO.setComplejidad(complejidadCombo
									.getSelectedIndex());// 1 alta,2 media, 3
															// baja
							caracteristicaDTO.setAlHorno(alHornojCheckBox1.isSelected());
							caracteristicaDTO.setTiempoCoccion(Double
									.parseDouble(tCoccionText.getText()));
							caracteristicaDTO.setId(idIngrediente);
							recetaDTO.setCaracteristicaDTO(caracteristicaDTO);
							final List<IngredienteDTO> ingredientes = new ArrayList<IngredienteDTO>();
							for (Vector<String> row : ((Vector<Vector<String>>) ingredienteModel
									.getDataVector())) {
								final ProductoDTO producto = new ProductoDTO();
								producto.setId(Long.valueOf(row.elementAt(0)));
								final IngredienteDTO ingrediente = new IngredienteDTO();
								ingrediente.setProductoDTO(producto);
								ingrediente.setCantidad(Double.valueOf(row
										.elementAt(1)));
								ingredientes.add(ingrediente);
							}
							recetaDTO.setIngredientes(ingredientes);
							recetaDTO.setId(idReceta);
							recetaDTO.setNombre(nombreText.getText());
							
							idReceta++;
							
							Sistema.get().getComidaControlador().altaReceta(recetaDTO);
							AltaReceta.this.dispose();
						}else{
							idRecetas.setText("Una o mas de las recetas ingresadas no existe");
						}
						}
					}

				});

			}
			{
				agregarIngredienteButton = new JButton();
				agregarIngredienteButton.setText("Agregar");
				agregarIngredienteButton
						.addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {
								ingredienteModel
										.addRow(new String[] { "", "" });
							}
						});
			}
			{
				scroll = new JScrollPane();
				getContentPane().add(scroll);
				scroll.setBounds(23, 101, 354, 141);
				{
					this.ingredienteModel = new DefaultTableModel();
					ingredienteModel.addColumn("ID Producto");
					ingredienteModel.addColumn("Cantidad");
					ingrediente = new JTable();
					scroll.setViewportView(ingrediente);
					ingrediente.setModel(ingredienteModel);
				}
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(16, 16)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(scroll, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)
				        .addGap(11))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(14)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(nombreLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				            .addComponent(nombreText, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE))
				        .addGap(21)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(tCoccionText, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
				            .addComponent(tCoccionLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 11, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(complejidadCombo, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(complejidadLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(alHornojCheckBox1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(idRecetas, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				            .addComponent(esCompuesta, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
				.addComponent(descripcionLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(agregarIngredienteButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				        .addGap(19)
				        .addComponent(altaRecetaButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				        .addGap(13))
				    .addComponent(descripcionArea, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(15, 15));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(esCompuesta, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(descripcionLabel, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
				                .addGap(11)))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(idRecetas, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(tCoccionLabel, GroupLayout.PREFERRED_SIZE, 201, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(tCoccionText, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
				        .addGap(21))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(nombreLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(complejidadLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				                .addGap(6)))
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(descripcionArea, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
				            .addGroup(thisLayout.createSequentialGroup()
				                .addPreferredGap(descripcionArea, complejidadCombo, LayoutStyle.ComponentPlacement.INDENT)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(complejidadCombo, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
				                        .addGap(37)
				                        .addComponent(alHornojCheckBox1, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(nombreText, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
				                        .addGap(12)))))
				        .addGap(20)))
				.addGap(38)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(0, 0, Short.MAX_VALUE)
				        .addComponent(scroll, GroupLayout.PREFERRED_SIZE, 354, GroupLayout.PREFERRED_SIZE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(0, 124, Short.MAX_VALUE)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(agregarIngredienteButton, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
				            .addComponent(altaRecetaButton, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))
				        .addGap(100)))
				.addContainerGap(29, 29));
			pack();
			this.setSize(724, 290);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

}
